package com.punai.system.service;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.domain.entity.SysDept;
import com.punai.common.utils.SecurityUtils;
import com.punai.system.bo.ProjectAuthBo;
import com.punai.system.domain.SysDeptLinkDept;
import com.punai.system.mapper.SysDeptLinkDeptMapper;
import com.punai.system.mapper.SysDeptMapper;
import com.punai.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PermissionService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysDeptLinkDeptMapper deptLinkDeptMapper;




    /**
     * 验证是否是操作数据的上级部门
     * @param operaDeptId 被操作数据的部门id
     */
    public boolean isOperaDataParentDept(Long operaDeptId) {
        // 当前登录的人的deptId
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = sysDeptMapper.selectById(operaDeptId);
        if (sysDept != null) {
            Long parentId = sysDept.getParentId();
            return Objects.equals(deptId, parentId);
        }
        return false;
    }

    /**
     * 获取当前用户登录的部门及下级部门
     * @return []
     */
    public List<Long> getChildrenDeptIds() {
        List<Long> resultDeptIds = new ArrayList<>();
        Long deptId = SecurityUtils.getDeptId();
        resultDeptIds.add(deptId);
        List<SysDept> sysDepts = sysDeptMapper.selectChildrenDeptById(deptId);
        if (CollUtil.isNotEmpty(sysDepts)) {
            for (SysDept sysDept : sysDepts) {
                resultDeptIds.add(sysDept.getDeptId());
            }
        }
        return resultDeptIds;
    }

    /**
     * 根基deptId 获取其子集
     * @param deptId
     * @return
     */
    public List<Long> getChildrenDeptIds(Long deptId) {
        List<Long> resultDeptIds = new ArrayList<>();
        resultDeptIds.add(deptId);
        List<SysDept> sysDepts = sysDeptMapper.selectChildrenDeptById(deptId);
        if (CollUtil.isNotEmpty(sysDepts)) {
            for (SysDept sysDept : sysDepts) {
                resultDeptIds.add(sysDept.getDeptId());
            }
        }
        return resultDeptIds;
    }

    /**
     * 二级集团获取权限部门
     */
    public List<Long> getDeptLinkDeptIds() {
        List<Long> resultIds = new ArrayList<>();
        Long deptId = SecurityUtils.getDeptId();
        //
        List<SysDeptLinkDept> linkDeptList = deptLinkDeptMapper.selectList(
                Wrappers.<SysDeptLinkDept>lambdaQuery()
                        .select(SysDeptLinkDept::getLinkId)
                        .eq(SysDeptLinkDept::getDeptId, deptId)
        );
        if (CollUtil.isNotEmpty(linkDeptList)) {
            for (SysDeptLinkDept sysDeptLinkDept : linkDeptList) {
                resultIds.add(sysDeptLinkDept.getLinkId());
            }
        }
        return resultIds;
    }


    public ProjectAuthBo getAuthorityBo() {
        ProjectAuthBo bo = new ProjectAuthBo();
        // 集团账号登录是否也可以如此
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            bo.setAuthUserName("");
            bo.setAuthDeptIds(null);
        } else {
            Set<Long> authorityDeptIds = getAuthorityDeptIds();
            String currentUserName = getCurrentUserName();
            bo.setAuthUserName(currentUserName);
            bo.setAuthDeptIds(authorityDeptIds);
        }
        return bo;
    }


    public Set<Long> getAuthorityDeptIds() {
        Set<Long> resultIds = new HashSet<>();
        Long deptId = SecurityUtils.getDeptId();
        if (deptId == null) {
            // 什么都查不出来
            resultIds.add(-1L);
        }
        SysDept sysDept = sysDeptMapper.selectById(deptId);
        if (sysDept != null) {
            String s = sysDept.gettGroup();
            if (StrUtil.equals(s,"Y")) {
                // 二级集团关联的部门
                List<Long> deptLinkDeptIds = getDeptLinkDeptIds();
                if (CollUtil.isNotEmpty(deptLinkDeptIds)) {
                    resultIds.addAll(deptLinkDeptIds);
                }
            }
            List<Long> childrenDeptIds = getChildrenDeptIds(deptId);
            if (CollUtil.isNotEmpty(childrenDeptIds)) {
                resultIds.addAll(childrenDeptIds);
            }
        }
        return resultIds;
    }

    public String getCurrentUserName() {
        String username = SecurityUtils.getUsername();
        // 可以做处理
        return username;
    }

}
